#include "robotlist.h"
#include "databasemanager.h"

#include "mainwindow.h"

#include "autoform.h"

#include <sitckcontrol/stickcontrol.h>

RobotList * RobotList::m_pInstance = NULL;

RobotList *RobotList::getInstance()
{
    if(m_pInstance == NULL)
        m_pInstance = new RobotList();
    return m_pInstance;
}

bool RobotList::updateRobot(int nID, QString strName,QString strUrdfName,
                            QString strSportIP,
                            int nSportPort,
                            QString strPerceptionIP,
                            int nPerceptionrPort,
                            QString strSlamIP,
                            int nSlamPort,
                            QString strSpeakIP,
                            int nSpeakPort,
                            QString strZeroIP,
                            int nZeroPort,
                            QString strTelemetryIP,
                            int nTelemetryPort,
                            int nLeftTool, int nRightTool,
                            int nHeadType,
                            QString strUser,
                            QString strPwd,
                            QList<VideoInfo> lsVideo, QString &strErr)
{
    if(DatabaseManager::getInstance()->updateRobot(nID, strName,strUrdfName,
                                                    strSportIP,
                                                    nSportPort,
                                                    strPerceptionIP,
                                                    nPerceptionrPort,
                                                    strSlamIP,
                                                    nSlamPort,
                                                    strSpeakIP,
                                                    nSpeakPort,
                                                    strZeroIP,
                                                    nZeroPort,
                                                    strTelemetryIP,
                                                    nTelemetryPort,
                                                    nLeftTool,nRightTool,nHeadType,
                                                    strUser,strPwd,
                                                    lsVideo, strErr))
    {
        Robot * pRobot = getRobotByID(nID);
        int nIndex = getIndexByID(nID);
        if(!pRobot)
        {
            strErr = tr("机器人不存在！");
            return false;
        }
        pRobot->update(strName,
                       strUrdfName,
                       strSportIP,
                       nSportPort,
                       strPerceptionIP,
                       nPerceptionrPort,
                       strSlamIP,
                       nSlamPort,
                       strSpeakIP,
                       nSpeakPort,
                       strZeroIP,
                       nZeroPort,
                       strTelemetryIP,
                       nTelemetryPort,
                       (ToolType)nLeftTool,(ToolType)nRightTool,(HeadType)nHeadType,
                       strUser,strPwd,
                       lsVideo);
        emit dataChanged(index(nIndex), index(nIndex));
        return true;
    }
    return false;
}

bool RobotList::addRobot(QString strName,
                         QString strUrdfName,
                         QString strSportIP,
                         int nSportPort,
                         QString strPerceptionIP,
                         int nPerceptionrPort,
                         QString strSlamIP,
                         int nSlamPort,
                         QString strSpeakIP,
                         int nSpeakPort,
                         QString strZeroIP,
                         int nZeroPort,
                         QString strTelemetryIP,
                         int nTelemetryPort,
                         int nLeftTool, int nRightTool,
                         int nHeadType,
                         QString strUser,
                         QString strPwd,
                         QList<VideoInfo> lsVideo, QString &strErr)
{
    //插入数据库
    int nID = 0;
    if(DatabaseManager::getInstance()->newRobot(strName,strUrdfName,
                                                 strSportIP,
                                                 nSportPort,
                                                 strPerceptionIP,
                                                 nPerceptionrPort,
                                                 strSlamIP,
                                                 nSlamPort,
                                                 strSpeakIP,
                                                 nSpeakPort,
                                                 strZeroIP,
                                                 nZeroPort,
                                                 strTelemetryIP,
                                                 nTelemetryPort,
                                                 nLeftTool,nRightTool,nHeadType,strUser,strPwd, lsVideo, nID, strErr))
    {
        beginInsertRows(QModelIndex(),m_lsRobot.size(),m_lsRobot.size());
        m_lsRobot.append(new Robot(nID,strName,strUrdfName,
                                   strSportIP,
                                   nSportPort,
                                   strPerceptionIP,
                                   nPerceptionrPort,
                                   strSlamIP,
                                   nSlamPort,
                                   strSpeakIP,
                                   nSpeakPort,
                                   strZeroIP,
                                   nZeroPort,
                                   strTelemetryIP,
                                   nTelemetryPort,
                                   (ToolType)nLeftTool,(ToolType)nRightTool,(HeadType)nHeadType,strUser,strPwd,lsVideo));
        m_lsChecked.append(false);
        //emit insertItem();
        endInsertRows();
        return true;
    }
    return false;
}





int RobotList::getIndexByID(int nID)
{
    int nIndex = -1;
    for(int i = 0; i < m_lsRobot.size(); ++i)
    {
        if(m_lsRobot[i]->nID()==nID)
        {
            return i;
        }
    }
    return nIndex;
}

Robot *RobotList::getRobotByIndex(int nIndex)
{
    if(nIndex>= 0 &&nIndex < m_lsRobot.size())
        return m_lsRobot[nIndex];
    else
        return NULL;
}

void RobotList::removeByIndex(int nIndex)
{
    if(nIndex < m_lsRobot.size())
    {
        QString strErr;
        if(DatabaseManager::getInstance()->deleteRobot(m_lsRobot[nIndex]->nID(), strErr))
        {
            beginRemoveRows(QModelIndex(),nIndex,nIndex);
            delete m_lsRobot[nIndex];
            m_lsRobot.removeAt(nIndex);
            m_lsChecked.removeAt(nIndex);
            endRemoveRows();
        }
    }
}

void RobotList::setCurRobot(Robot *robot)
{
    if(m_pCurRobot != robot)
    {
        Robot * pLastRobot = m_pCurRobot;
        m_pCurRobot=robot;
        emit curRobotChanged(m_pCurRobot,pLastRobot);
    }
}

RobotList::RobotList(QObject *parent)
    : QAbstractListModel{parent}
{
    QString strErr;
    //加载机器人列表
    DatabaseManager::getInstance()->getRobotList(m_lsRobot,strErr);
    for(int i = 0; i < m_lsRobot.size(); ++i)
    {
        m_lsChecked.append(false);
    }
}

RobotList::~RobotList()
{
    for(int i = 0; i < m_lsRobot.size(); ++i)
    {
        delete m_lsRobot[i];
    }
    m_lsRobot.clear();
    m_lsChecked.clear();
}

Robot *RobotList::getRobotByID(int nRobotID)
{
    for(int i = 0; i < m_lsRobot.size(); ++i)
    {
        if(m_lsRobot[i]->nID()==nRobotID)
        {
            return m_lsRobot[i];
        }
    }
    return nullptr;
}

Robot *RobotList::getCurRobot()
{
    return m_pCurRobot;
}

int RobotList::getCount()
{
    return m_lsRobot.size();
}

void RobotList::setCheckByIndex(int nIndex, bool bCheck)
{
    if(nIndex < m_lsChecked.size())
    {
        m_lsChecked[nIndex] = bCheck;
        emit dataChanged(index(nIndex), index(nIndex));
    }
}



int RobotList::rowCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent);
    return m_lsRobot.size();
}

QVariant RobotList::data(const QModelIndex &index, int role) const
{
    if (!index.isValid()||m_lsRobot.size()<=index.row())
        return QVariant();
    if (role == NameRole) {
        return m_lsRobot[index.row()]->strName();
    }
    if(role == CheckRole)
    {
        return m_lsChecked[index.row()];
    }
    return QVariant();
}

